package com.example.docker_demo_connect_database.dao;

import com.example.docker_demo_connect_database.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class UserDao implements IUserDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public void insert(int id, String name){
        this.jdbcTemplate.execute("insert into user (name) value ('"+name+"')");
        if(true){
            //throw new RuntimeException("test");
        }
    }

    @Override
    public List<User> select(int id) {
        return this.jdbcTemplate.query("select * from user where id="+id,new BeanPropertyRowMapper(User.class));
    }
}
